Repository navigation
fix(observability): six of seven alert rules could never fire - #416
Open
Matthew-Selvam wants to merge 2 commits into
Open
Matthew-Selvam wants to merge 2 commits into
Matthew-Selvam wants to merge 2 commits into
Conversation
The default alert rules read ten metric names. Only two had a producer: balance_cents, survival_tier <- set in heartbeat/tasks.ts report_metrics context_tokens_total <- no producer heartbeat_task_failures_total <- no producer heartbeat_task_successes_total <- no producer policy_denies_total <- no producer policy_decisions_total <- no producer inference_cost_cents <- no producer turns_last_hour <- no producer turns_total <- no producer unhealthy_child_count <- no producer Every absent name hit the rule's `?? 0` / `?? -1` fallback, so the condition returned false and the rule could never fire. Only `balance_below_reserve` and `context_near_capacity` were live. The alerting system was inert in production while its unit tests passed, because those tests asserted only that the rules EXISTED, never that they fire. report_metrics now derives the missing metrics from tables the agent already writes (turns, policy_decisions, inference_costs, heartbeat_history, children). Each query is independent and individually wrapped, so an unavailable table degrades one metric rather than failing the tick. Publishing them as gauges rather than counters is deliberate: these are absolute DB totals recomputed each tick, so `increment()` would re-add the same total on every tick and inflate it without bound. The six rules that read them via `counters.get()` were updated to `gauges.get()` to match. Four existing tests in observability.test.ts constructed snapshots with these names under `counters`, which would now pass vacuously. Repinned in this same change to publish them as gauges, so "does not fire" really tests the threshold logic instead of a missing key. `context_tokens_total` is deliberately NOT wired: it measures the agent's context-window fill, which only the inference loop can observe. Reporting a constant would be worse than not reporting it. Left as-is, and the rule stays inert until someone instruments buildContextMessages - flagged here rather than guessed at. New src/__tests__/alert-rules-wiring.test.ts (13 tests) asserts each rule FIRES against realistic values, that a healthy system fires nothing, that the policy rule respects its sample-size floor, and that the derived SQL works against the real schema. RED check: 3 fail against the unfixed alerts.ts. Verified: typecheck clean; 181 tests across observability, alert-rules-wiring, heartbeat, heartbeat-scheduler and lifecycle pass.
Matthew-Selvam
force-pushed
the
fix/alert-rules-read-unproduced-metrics
branch
from
October 1, 2026 08:42
8664f7c to
1e3a9f0
Compare
The context_near_capacity alert rule reads context_tokens_total but no code was publishing it. This renders the alert inert in production. Instrument buildContextMessages to estimate and publish the current context token count, enabling the alert to fire when context approaches budget limits.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The default alert rules read ten metric names. Only two had a producer. Every absent name hit its rule's
?? 0/?? -1fallback, so the condition returned false and the rule could never fire:balance_cents,survival_tierheartbeat/tasks.tsreport_metricsturns_last_hour,turns_totalheartbeat_task_failures_total,heartbeat_task_successes_totalpolicy_denies_total,policy_decisions_totalinference_cost_centsunhealthy_child_countcontext_tokens_totalOnly
balance_below_reserveandcontext_near_capacitywere live. The alerting system was inert in production while its unit tests passed — those tests asserted only that the rules existed, never that they fire.The fix
report_metricsnow derives the missing metrics from tables the agent already writes —turns,policy_decisions,inference_costs,heartbeat_history,children. Each query is independent and individually wrapped, so one unavailable table degrades a single metric rather than failing the tick.Gauges, not counters, deliberately. These are absolute DB totals recomputed each tick, so
increment()would re-add the same total every tick and inflate it without bound. The six rules that read them viacounters.get()were updated togauges.get()to match.Repinned existing tests
Four tests in
observability.test.tsbuilt snapshots with these names undercounters. After the change they would pass vacuously — asserting a missing key produces0. Repinned in the same change to publish them as gauges, so "does not fire" actually tests the threshold logic.Deliberately not wired:
context_tokens_totalThis one measures context-window fill, which only the inference loop can observe. Reporting a constant would be worse than not reporting it, so the rule stays inert until someone instruments
buildContextMessages. Flagged rather than guessed at — happy to do that as a follow-up if you want it.Testing
src/__tests__/alert-rules-wiring.test.ts(new, 13 tests) asserts each rule fires against realistic values, that a healthy system fires nothing, that the policy rule respects its 10-sample floor, and that the derived SQL works against the real schema.Verification
pnpm typecheck— clean